﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace parkingclient
{
    public partial class admin_res : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //if user is not an admin, redirect to main page
            if (Session["userType"] == null || int.Parse(Session["userType"].ToString()) != 3)
            {
                Response.Redirect("Default.aspx");
            }

            if (Session["user"] == null)
            {
                lblWelcome.Text = "[<a href='user_login.aspx'>Login</a>]   [<a href='registration.aspx'>Register</a>]";
            }
            else if (int.Parse(Session["userType"].ToString()) == 3) //for admin, add link to the admin dashboard
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='admin_dashboard.aspx'>Admin Dashboard</a>]    [<a href='user_logout.aspx'>Logout</a>]";
            }
            else
            {
                lblWelcome.Text = "Welcome " + Session["user"] + "   [<a href='user_logout.aspx'>Logout</a>]";
            }

            //construct the html table header
            string s = "<table border=1<tr><th>Reservation ID</th><th>User ID</th><th>Email</th><th>Start Time</th><th>End Time</th><th>Spot ID</th><th>Actions</th></tr>";


            //load the reservation table
            ParkingService.WebService1 parking = new ParkingService.WebService1();
            string resXml = parking.getAllReservationList();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(resXml);

            XmlNodeList resList = xmlDoc.GetElementsByTagName("reservation");

            foreach (XmlNode r in resList)
            {
                string resid = r.Attributes["id"].Value;
                string userid = r["userid"].InnerText;
                string email = r["email"].InnerText;
                string starttime = r["starttime"].InnerText;
                string endtime = r["endtime"].InnerText;
                string spotId = r["spotId"].InnerText;


                //add this reservation to the table
                s += "<tr><td>" + resid + "</td><td>" + userid + "</td><td>" + email + "</td><td>" + starttime + "</td><td>" + endtime + "</td><td>" + spotId;

                //add the buttons
                s += "</td><td><input type='button' onclick=\"location.href='res_delete.aspx?resid=" + resid + "'\" value='Delete'></td></tr>";

            }



            //close the html table
            s += "</table>";

            //add to the page
            lblRes.Text = s;

        }
    }
}
